package com.john.blog.common.vo;

import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 请求响应常量 包括响应码和响应提示
 * 
 * @author JiangGengchao
 * @date 2018年2月4日
 */
public class CommonResult {
    /**
     * 响应码常量类
     *
     */
    public static class Code {
        /**
         * 请求成功
         */
        public static final int SUCCESS = 200;
        /**
         * 请求失败
         */
        public static final int FAILURE = 1001;
        /**
         * 静态化失败
         */
        public static final int STATICS_ERROR = 1002;
        /**
         * 已存在
         */
        public static final int EXISTS = 1003;
        /**
         * 服务器端异常
         */
        public static final int SERVER_ERROR = 5000;

        public static final int USER_ERROR = 2000;

        public static final int PASS_ERROR = 2001;

        public static final int CODE_ERROR = 2002;

        public static final int NO_AUTH = 3000;
    }

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    // 响应业务状态
    private Integer status;

    // 响应消息
    private String msg;

    // 响应中的数据
    private Object data;

    private CommonResult(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public static CommonResult success() {
        return new CommonResult(CommonResult.Code.SUCCESS, null, null);
    }

    public static CommonResult success(Object data) {
        return new CommonResult(CommonResult.Code.SUCCESS, null, data);
    }

    public static CommonResult error() {
        return new CommonResult(CommonResult.Code.CODE_ERROR, null, null);
    }

    public static CommonResult error(String msg) {
        return new CommonResult(CommonResult.Code.CODE_ERROR, msg, null);
    }

    public static CommonResult build(Integer status) {
        return new CommonResult(status, null, null);
    }

    public static CommonResult build(Integer status, String msg, Object data) {
        return new CommonResult(status, msg, data);
    }

    public static CommonResult build(Integer status, String msg) {
        return new CommonResult(status, msg, null);
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 将json结果集转化为TaotaoResult对象
     * 
     * @param jsonData
     *            json数据
     * @param clazz
     *            TaotaoResult中的object类型
     * @return
     */
    public static CommonResult formatToPojo(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                return MAPPER.readValue(jsonData, CommonResult.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (clazz != null) {
                if (data.isObject()) {
                    obj = MAPPER.readValue(data.traverse(), clazz);
                } else if (data.isTextual()) {
                    obj = MAPPER.readValue(data.asText(), clazz);
                }
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 没有object对象的转化
     * 
     * @param json
     * @return
     */
    public static CommonResult format(String json) {
        try {
            return MAPPER.readValue(json, CommonResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Object是集合转化
     * 
     * @param jsonData
     *            json数据
     * @param clazz
     *            集合中的类型
     * @return
     */
    public static CommonResult formatToList(String jsonData, Class<?> clazz) {
        try {
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (data.isArray() && data.size() > 0) {
                obj = MAPPER.readValue(data.traverse(), MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }
}
